<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.orderId" placeholder="单号查找" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.serialNum" placeholder="流水号查找" clearable></el-input>
      </el-form-item>
      <!--      <el-form-item>-->
      <!--        <el-input v-model="dataForm.teacherName" placeholder="老师名字" clearable></el-input>-->
      <!--      </el-form-item>-->

      <el-form-item>
        <span>课程类型:</span>
        <el-select style="width: 120px" v-model="dataForm.courseType" placeholder="请选择">
          <el-option
            v-for="item in options2"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span>订单状况:</span>
        <el-select style="width: 120px" v-model="dataForm.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item>
        <!--        <el-button @click="getDataList()">查询</el-button>-->
        <el-button v-if="isAuth('modules:courseorder:save')" type="primary" @click="getDataList()">查询</el-button>
        <!--        <el-button v-if="isAuth('modules:courseorder:delete')" type="danger" @click="refundHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="id"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="orderId"
        header-align="center"
        align="center"
        label="自定义订单号">
      </el-table-column>
      <el-table-column
        prop="userId"
        header-align="center"
        align="center"
        width="70px"
        label="用户id">
      </el-table-column>
      <el-table-column
        prop="username"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>

      <!--      <el-table-column-->
      <!--        prop="courseId"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="70px"-->
      <!--        label="课程id">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="courseTitle"
        header-align="center"
        align="center"
        width="180px"
        label="课程名字">
      </el-table-column>
      <el-table-column
        prop="realName"
        header-align="center"
        align="center"
        label="老师名字">
      </el-table-column>

      <el-table-column
        prop="courseType"
        header-align="center"
        align="center"
        label="课程类型">
        <!--        label="课程类型【0录播课，1直播课】"-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.courseType === 0">录播课</el-tag>
          <el-tag v-if="scope.row.courseType === 1" style="background-color: #5cc0f5;color: white">直播课</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="payPrice"
        header-align="center"
        width="90px"
        align="center"
        label="支付价格">
      </el-table-column>

      <el-table-column
        prop="serialNum"
        header-align="center"
        align="center"
        label="支付公司方提供的流水号">
      </el-table-column>


      <!--      <el-table-column-->
      <!--        prop="showStatus"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="逻辑删除位【0删除，1存在】">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="updateTime"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        label="更新时间">-->
      <!--      </el-table-column>-->
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="订单状态">
        <!--        label="订单状态【0新建未支付，1已支付，2申请退款，3已退款】"-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0">未支付</el-tag>
          <el-tag v-if="scope.row.status === 1" style="background-color: #5cc0f5;color: white">已支付</el-tag>
          <el-tag v-if="scope.row.status === 2" style="background-color: #f8839d;color: white">申请退款</el-tag>
          <el-tag v-if="scope.row.status === 3" style="background-color: #ffab2e;color: white">已退款</el-tag>

        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        prop="payMode"-->
      <!--        header-align="center"-->
      <!--        align="center"-->
      <!--        width="70px"-->
      <!--        label="支付方式">-->


      <!--        label="支付方式【'微信','支付宝'】"-->
      <!--      </el-table-column>-->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="70px"
        label="详情">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row)">详情</el-button>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">详情</el-button>-->
          <!--          <el-button style="background-color: #ff7474;color: #fffcfc" v-if="scope.row.status === 1" size="small"-->
          <!--                     @click="refundHandle(scope.row.id)">退款-->
          <!--          </el-button>-->
          <el-button style="background-color: #2ed9bf;color: #fffcfc" v-if="scope.row.status === 2" size="small"
                     @click="clickRefund(scope.row.orderId,1)">同意退款
          </el-button>
          <el-button style="margin-right:10px;margin-top:4px;background-color: #ff6565;color: #fffcfc"
                     v-if="scope.row.status === 2" size="small" @click="clickRefund(scope.row.orderId,2)">拒绝退款
          </el-button>
          <el-button style="margin-right:10px;margin-top:4px;background-color: #ffab2e;color: #fffcfc"
                     @click="showDiaLog1(scope.row)"
                     v-if="scope.row.status === 3" size="small">退款记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <course-order ref="refund"
    >

    </course-order>
    <!--    <el-dialog :visible.sync="showRefund">-->
    <!--      <el-input>-->

    <!--      </el-input>-->
    <!--    </el-dialog>-->
    <!--    退款对话框-->
    <el-dialog title="确认退款" :visible.sync="showDiaLog" @close="disShow" v-if="showDiaLog" style="text-align: center">
      <!--      <el-input v-model="dataForm.mark" autocomplete="off"></el-input>-->
      <el-input
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4}"
        placeholder="请输入退款情况"
        v-model="refundForm.mark">
      </el-input>

      <multi-upload
        style="margin-top: 5%"
        :value="dataForm.photos"
        @getPhotos="getPhotos"
        @disShow="disShow()"
        @getMark="getMark"
      >
      </multi-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="disShow">取 消</el-button>
        <el-button type="primary" @click="refundHandle">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import multiUpload from '../../../components/upload/multiUpload'
import CourseOrder from './course-order'
import AddOrUpdate from './courseorder-add-or-update'
import MultiUpload from '../../../components/upload/multiUpload'

export default {
  data () {
    return {

      dataForm: {
        orderId: '',
        courseType: '',
        status: '',
        serialNum: '',
        teacherName: '',
        photos: [],

      },
      refundForm: {
        orderId: '',
        mark: '',
        photos: [],
        agree: 0,
      },
      userId: '',
      photo: [],
      showDiaLog: false,
      showRefund: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      options2: [
        {
          value: '',
          label: '全部课程'
        },
        {
          value: 1,
          label: '直播课程'
        }, {
          value: 0,
          label: '录播课程'
        },],
      options: [
        {
          value: '',
          label: '全部状态'
        },
        {
          value: 0,
          label: '未支付'
        }, {
          value: 1,
          label: '已支付'
        }, {
          value: 2,
          label: '申请退款'
        }, {
          value: 3,
          label: '已退款'
        }]
    }
  },
  components: {
    MultiUpload,
    AddOrUpdate,
    CourseOrder
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/course/courseorder/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'orderId': this.dataForm.orderId,
          'serialNum': this.dataForm.serialNum,
          'courseType': this.dataForm.courseType,
          'status': this.dataForm.status,
          // 'teacherName':this.dataForm.teacherName
        })
      }).then(({data}) => {
        if (data && data.code === 0) {

          this.dataList = data.page.list
          this.totalPage = data.page.totalCount

        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },

    getMark (value) {
      //给退款表单赋值
      this.refundForm.mark = value
    },
    //上传的图片地址
    getPhotos (value) {
      //给退款表单赋值
      this.refundForm.photos = value
      // console.log(this.dataForm.photos)
    },
    //退款取消按钮
    disShow () {
      this.refundForm.mark = ''
      this.showDiaLog = false
      this.refundForm.photos = []
    },
    showDiaLog1 (order) {
      this.$nextTick(() => {
        this.$refs.refund.init(order)
      })

    },
    //点击退款
    clickRefund (orderId, agree) {
      //给退款表单赋值
      this.refundForm.orderId = orderId
      //根据拒绝或者同意给表单赋值
      this.refundForm.agree = agree
      this.showDiaLog = true
    },
    // 退款或者拒绝退款
    refundHandle () {
      // let ids = id ? [id] : this.dataListSelections.map(item => {
      //   return item.id
      // })
      //后端需要的数据
      const params = {
        photos: this.refundForm.photos,
        agree: this.refundForm.agree,
        orderId: this.refundForm.orderId,
        mark: this.refundForm.mark
      }
      // this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '退款' : '批量退款'}]操作?`, '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      this.$confirm(`确定进行退款操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/sys/refund/agree/refund/`),
          method: 'post',
          data: this.$http.adornData(params)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.showDiaLog = false
                this.refundForm.mark = ''
                this.refundForm.photos = ''
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },

  }
}
</script>
